﻿
#include "Business/businessinit.h"

#include "Network/network.h"
#include <iostream>
#include <FileManager/filemanager.h>
#include <sstream>
#include <CipherUtils/cipherutils.h>
#include "Business/businesssql.h"

#ifndef UNITTEST

int main(int argc, char *argv[])
{
    BusinessInit& Binit = BusinessInit::getInstance();
    if (!Binit.InitModules()) {
        throw "init modules error";
        return 0;
    }
    Binit.InitProgram();
    // 基础的模块都初始化完以后开始初始化数据库
    BusinessSQL::getInstance().init();
    BusinessSQL::getInstance().useDatabase();

    Network& net = Network::getInstance();

    net.start();

    std::cout << "start running" << std::endl;


    while(1) {
        std::string input;
        // 获取整行输入
        std::getline(std::cin, input);

        std::istringstream iss(input);
        std::string command, username, password;

        // 首先提取命令部分
        iss >> command;

        // 检查命令是否为"createUser"
        if (command == "createUser") {
            // 提取用户名和密码
            iss >> username >> password;
            // 检查是否成功提取了用户名和密码
            if (!username.empty() && !password.empty()) {

                std::string shaPassword = CipherUtils::getInstance().SHA256_calculate(password);
                BusinessSQL::getInstance().createAUser(username, shaPassword);
                std::cout << "create user success!!!"<< std::endl;
            } else {
                std::cout << "Invalid input format. Expected format: createUser [username] [password]" << std::endl;
            }
        } else if (command == "stop") {
            break;
        } else {
            std::cout << "input \"stop\" to end the program\ninput \"createUser [username] [password]\" to create a new user" << std::endl;

        }
    }

    net.stop();
    FileManager::getInstance().stop();
    return 0;
}

#else


#endif
